const path = require("path");
require("dotenv").config({
  override: true,
  path: path.resolve(__dirname, ".env"),
});

const express = require("express");
const cors = require("cors");
const router = require("./routes/index.js");
const sequelize = require("./db");
const cookieParser = require("cookie-parser");
const error = require("./middleware/errorMiddleware.js");

const PORT = process.env.PORT;

const app = express();

var corsOptions = {
  credentials: true,
  origin: process.env.CLIENT_URL,
  optionsSuccessStatus: 200,
};

app.use(express.json());
app.use(cookieParser());
app.use(cors(corsOptions));
app.use("/api", router);
app.use(error);

const start = async () => {
  try {
    //-------------------------подключение к базе-----------------------------
    await sequelize.authenticate();
    await sequelize.sync();
    //-------------------------запуск сервера-----------------------------
    app.listen(PORT, () => {
      console.log(`Server is running on port http://localhost:${PORT}`);
    });
    console.log("Connection has been established successfully.");
  } catch (error) {
    console.error("Unable to connect to the database:", error);
  }
};
start();

// const httpsServer = https.createServer(credentials, app);
//const httpServer = http.createServer(app);

//const https = require("https");
//const http = require("http");
//const fs = require("fs");
// const privateKey = fs.readFileSync("sslcert/server.key", "utf8");
// const certificate = fs.readFileSync("sslcert/server.crt", "utf8");
// const credentials = { key: privateKey, cert: certificate };
